﻿using System;
using System.IO.Ports;


namespace BusinessDll
{
    public class COM
    {
        public event PortDataReceivedEventHandle Received;
        public event SerialErrorReceivedEventHandler Error;
        public SerialPort port;
        public bool ReceiveEventFlag = false;  //接收事件是否有效 false表示有效

        public byte[] msg_head { set; get; }//数据头
        public byte[] msg_end { set; get; }//数据尾
        public int msg_len { set; get; }//数据长度
        public bool isMsgHead { set; get; }//数据长度
        public byte[] MSG { set; get; }//称重数据

        public COM(string sPortName, int baudrate)
        {
            port = new SerialPort(sPortName, baudrate, Parity.None, 8, StopBits.One)
            {
                RtsEnable = true,
                ReadTimeout = 300
            };
            port.DataReceived += new SerialDataReceivedEventHandler(DataReceived);
            port.ErrorReceived += new SerialErrorReceivedEventHandler(ErrorEvent);
            this.isMsgHead = false;
            this.MSG = new byte[1];
        }

        ~COM()
        {
            Close();
        }
        public void Open()
        {
            try
            {
                if (!port.IsOpen)
                {
                    port.Open();
                }
            }
            catch (Exception)
            {

            }
        }

        public void Close()
        {
            if (port.IsOpen)
            {
                port.Close();
            }
        }

        public string getDatea()
        {
            if (port.IsOpen)
            {
                return port.ReadExisting();
            }
            else
            {
                return null;
            }
        }

        //读取命令
        public int ReadCommand()
        {
            try
            {
                port.Open();
                while (true)
                {
                    if (port.IsOpen)
                    {
                        if (port.BytesToRead > this.msg_len)
                        {
                            this.MSG = new byte[this.msg_len];
                            port.Read(this.MSG, 0, this.msg_len);
                            if (this.MSG[0] == this.msg_head[0] && this.MSG[1] == this.msg_head[1])
                            {
                                port.Close();
                                return 1;
                            }
                        }
                    }
                    else
                    {
                        port.Open();
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
                return -1;
            }

        }

        public void ErrorEvent(object sender, SerialErrorReceivedEventArgs e)
        {
            if (Error != null)
            {
                Error(sender, e);
            }
        }
        //数据接收
        public void DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

        }

        public bool IsOpen()
        {
            return port.IsOpen;
        }
    }
    public delegate void PortDataReceivedEventHandle(object sender, PortDataReciveEventArgs e);
    public class PortDataReciveEventArgs : EventArgs
    {
        public PortDataReciveEventArgs()
        {
            data = null;
        }

        public PortDataReciveEventArgs(byte[] data)
        {
            this.data = data;
        }

        private byte[] data;

        public byte[] Data
        {
        	get { return data; }
        	set { data = value; }
        }
    }
}